package com.lnj.orders.controller;

import com.lnj.bean.model.Coupon;
import com.lnj.orders.service.CouponService;
import com.lnj.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/coupon")
public class CouponController {
    @Autowired
    private CouponService couponService;

    @GetMapping("/list")
    public ResponseEntity<ResponseResult> getAllCouponList(){
        List<Coupon> couponList = couponService.getAllCouponList();
        if (couponList == null || couponList.isEmpty()){
            return ResponseEntity.status(HttpStatus.NOT_FOUND)
                    .body(ResponseResult.ok("优惠券列表为空"));
        }

        return ResponseEntity.status(HttpStatus.CREATED)
                .body(ResponseResult.ok("优惠券列表查询成功").setData(couponList));
    }

    @GetMapping("/receive")
    public ResponseEntity<ResponseResult> getCoupon(@RequestHeader("X-User-ID") Long userId,
                                                    @RequestParam("couponId") Long couponId,
                                                    @RequestParam("limit") Integer limit){
        Integer i = couponService.receiveCoupon(userId, couponId, limit);
        if (i==null){
            return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                    .body(ResponseResult.ok("系统异常"));
        }
        if (i==0){
            return ResponseEntity.status(HttpStatus.OK)
                    .body(ResponseResult.ok("qw"));
        }
        if (i==1){
            return ResponseEntity.status(HttpStatus.OK)
                    .body(ResponseResult.ok("ylq"));
        }
        if (i==2){
            return ResponseEntity.status(HttpStatus.CREATED)
                    .body(ResponseResult.ok("lq"));
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                .body(ResponseResult.ok("系统繁忙"));
    }
}
